package Hot100.leetcode.editor.cn.code.数组;
//Java：最大子数组和
public class MaximumSubarray{
    public static void main(String[] args) {
        Solution solution = new MaximumSubarray().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int maxSubArray(int[] nums) {
        if(nums ==null || nums.length == 0){
            return 0;
        }
        //前一个元素结尾的最大子数组和用于两种情况+遍历的当前元素比较当前元素
        int pre = nums[0];
        int maxSum = pre;

        for (int i = 1; i < nums.length; i++) {
            //要么pre+当前>当前 加入前面的子数组 要么<当前则自己单独成一个数组重新开始
            int current = Math.max(pre+nums[i], nums[i]);
            //更新最大值
            maxSum = Math.max(maxSum, current);
            //更新因为两种情况可能前面的子数组加了当前这个元素继续变大也可能直接从当前这个元素开始了！
            pre = current;//无论怎样都得为下次迭代做准备

        }

        return maxSum;

    }
}
//leetcode submit region end(Prohibit modification and deletion)

}